//
//  UserInforTableViewController.m
//  ksjv2
//
//  Created by mac on 14/12/17.
//  Copyright (c) 2014年 CnbizMedia. All rights reserved.
//
//---------------------------------------------------------
//                    *              *
//                  *      个人信息     *
//                    *              *
//---------------------------------------------------------
#import "UserInforTableViewController.h"
#import "Util.h"
#import "AppDelegate.h"
#import "WebViewController.h"
@interface UserInforTableViewController (){
//    UIView*HeaderView;
    NSString*point;
}
@property (weak, nonatomic) IBOutlet UIImageView *sex;
@property (weak, nonatomic) IBOutlet UIButton *LV;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UIImageView *userIcon;
@property (weak, nonatomic) IBOutlet UIButton *Signature;

@end

@implementation UserInforTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIButton *buttonf=[UIButton buttonWithType:UIButtonTypeCustom];
    [buttonf setFrame:CGRectMake(0, 0, 22, 22)];
    [buttonf addTarget:self action:@selector(popOfDismss) forControlEvents:UIControlEventTouchUpInside];
    [buttonf setImage:[UIImage imageNamed:@"dismiss"] forState:UIControlStateNormal];
    
    NSString *newOldYear = [UserDefaults objectForKey:@"NewYear" ];
    if ([newOldYear isEqualToString:@"NewYear"]) {
        //状态栏的文字颜色换成白色
        //        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        
        //            [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
        [buttonf setImage:[UIImage imageNamed:@"dismissGray"] forState:UIControlStateNormal];
        
    }
 
    UIBarButtonItem*leftBarItem = [[UIBarButtonItem alloc]initWithCustomView:buttonf];
    self.navigationItem.leftBarButtonItem=leftBarItem;
}
-(void)viewWillAppear:(BOOL)animated{
    [self initTableHeader];
}
-(void)popOfDismss{
    if ([[self.navigationController viewControllers] count]>=2) {
        [self.navigationController popViewControllerAnimated:YES];
        
    }else{
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
- (void)profile:(id)sender {
    
    [self performSegueWithIdentifier:@"ToProfile" sender:self];
//    [self performSegueWithIdentifier:@"userPpro" sender:self];
}
-(void)initTableHeader{
    self.title=@"个人信息";
    self.tableView.backgroundColor=UIColorFromRGB(0xf5f5f5);

    self.tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
    
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 0, 40, 40)];
    [button setTitle:@"编辑" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(profile:) forControlEvents:UIControlEventTouchUpInside];
 
    UIBarButtonItem*rightBarItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.rightBarButtonItem=rightBarItem;
    NSDictionary*dic=[UserDefaults objectForKey:@"user"];
    if ([dic[@"sex"] intValue]==0) {
        self.sex.image=[UIImage imageNamed:@"women"];
    }else{
    self.sex.image=[UIImage imageNamed:@"men"];
    }
    [self.userIcon.layer setCornerRadius:30];
    [self.userIcon.layer setMasksToBounds:YES];
    
    [self.Signature.layer setCornerRadius:15];
    [self.Signature.layer setMasksToBounds:YES];
    [self.Signature setTitle:dic[@"signature"] forState:UIControlStateNormal];
    self.Signature.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;;
    self.Signature.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    point=dic[@"point"];
    self.name.text=dic[@"nickname"];
    
    [self.LV setTitle:[NSString stringWithFormat:@"LV%@",dic[@"vip"]] forState:UIControlStateNormal];
    
    NSString *url = [NSString stringWithFormat:@"%@",dic[@"thumb"][@"180"]];
    [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:url] options:SDWebImageRefreshCached progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        if (image==nil) {
            [self.userIcon setImage:[UIImage imageNamed:@"icon_default_ipad"]];
        }else{
            [self.userIcon setImage:image];
        }
    }];
    [self.tableView reloadData];
}
-(void)UserExit{
    AppDelegate*app=APPDelegate;
    app.BadgeStr=@"0";
    [[AppAPIClient sharedClient] setTokenA:@""];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"token"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"user"];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section==0) {
        return 1;
    }else if (section==1){
        return 1;
    }else{
        return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40;
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 10)];
    view.backgroundColor=UIColorFromRGB(0xf5f5f5);
    
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"inforcell" forIndexPath:indexPath];
    if (cell==nil) {
        cell=[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"inforcell"];
        
    }
    UILabel*label=(UILabel*)[cell.contentView viewWithTag:11];
    if (indexPath.section==0){
        
        cell.textLabel.text=[NSString stringWithFormat:@"积分:%@分",point];
        label.text=@"";
    }else if (indexPath.section==1){
        cell.textLabel.text=@"等级:1级";
        label.text=@"普通用户";
        [cell.contentView addSubview:label];
    }
    [cell setUserInteractionEnabled:NO];
    [cell setAccessoryType:UITableViewCellAccessoryNone];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setSelected:YES animated:NO];
    if (indexPath.section==0){
//        [self performSegueWithIdentifier:@"inforPintegral" sender:self];
        [self performSegueWithIdentifier:@"userPweb" sender:self];
    }else if (indexPath.section==1){
//        [self performSegueWithIdentifier:@"userPweb" sender:self];
    }
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    UIViewController *destination = segue.destinationViewController;
    if([destination isKindOfClass:[WebViewController class]]){
        [destination setTitle:@"我的积分"];
    }
}
@end
